package com.hacettepe.yemekhane;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class HtmlGetter {
	public ArrayList<TekYemek> aylikListe() {
		URL url = null;
		try {
			url = new URL(
					"http://www.mekya.com/hacettepe_yemekhane/hacettepe.php");
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		URLConnection u = null;
		try {
			u = url.openConnection();
		} catch (IOException e) {
			e.printStackTrace();
		}

		Reader in = null;

		try {
			in = new BufferedReader(new InputStreamReader(u.getInputStream(),
					"ISO-8859-9"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		String totalPage = "";
		while (true) {
			String line = null;
			try {
				line = ((BufferedReader) in).readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (line == null)
				break;
			totalPage += line + "\n";
		}
		totalPage = correctHtml(totalPage);
		ArrayList<TekYemek> yemekList = findAylik(totalPage);
		return yemekList;
	}

	public static ArrayList<TekYemek> findAylik(String s) {
		ArrayList<TekYemek> list = new ArrayList<TekYemek>();
		StringTokenizer token = new StringTokenizer(s, "\n\t");
		while (token.hasMoreTokens()) {
			String date = token.nextToken();
			if(isDate(date,"dd.MM.yyyy")){
				String yemek = charCorrectHtml(token.nextToken()) + "</br>";
				String kalori = token.nextToken();
				String gun = token.nextToken();
				yemek += charCorrectHtml(token.nextToken()) + "</br>";
				yemek += charCorrectHtml(token.nextToken()) + "</br>";
				yemek += charCorrectHtml(token.nextToken()) + "</br>";
				list.add(new TekYemek(date, yemek, kalori, gun));
			}
		}
		return list;
	}

	public static String correctHtml(String s) {
		String sonHtml = "";
		String sonHtml2 = "";
		String satir;
		StringTokenizer tokenizer = new StringTokenizer(s, "\n\t");
		while (tokenizer.hasMoreTokens()) {
			satir = tokenizer.nextToken().trim();
			sonHtml += satir + "\n";
		}
		StringTokenizer tokenizer2 = new StringTokenizer(sonHtml, "\n");
		while (tokenizer2.hasMoreTokens()) {
			satir = tokenizer2.nextToken();
			sonHtml2 += satir + "\n";
		}
		return sonHtml2;
	}

	public static String charCorrectHtml(String s) {
		String last = "";
		StringTokenizer token = new StringTokenizer(s, "Â");
		while (token.hasMoreTokens()) {
			last += token.nextToken();
		}
		StringBuffer last2 = new StringBuffer();
		last2.append(last);
		for (int j = 0; j < last2.length(); j++) {
			if (last2.charAt(j) == 'Ş') {
				last2.deleteCharAt(j + 1);
			}

		}

		return last2.toString();

	}
	public static boolean isDate(String text, String date_pattern) {
	    String newDate = text.replace("T00:00:00", "");
	    SimpleDateFormat formatter = new SimpleDateFormat(date_pattern);
	    ParsePosition position = new ParsePosition(0);
	    formatter.setLenient(false);
	    return formatter.parse(newDate, position) != null;
	}
}